<?PHP
	/************************************************************************
	*       This file, in addition to all other parts of X7 Chat are        *
	*                   Copyright 2003-2007 by Tim Chamness.                *
	* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
	* Version:          X7 Chat 3.0.0-A2                                    *
	* Release Date:     October 20, 2007                                    *
	* Author:           Tim Chamness                                        *
	* Copyright:        2003-2007 by Tim Chamness                           *
	* Website:          http://www.x7chat.com/                              *
	* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
	* X7 Chat 3 is free software; you can redistribute it and/or modify     *
	* it under the terms of the GNU General Public License as published by  *
	* the Free Software Foundation; either version 3 of the License, or     *
	* (at your option) any later version.                                   *
	*                                                                       *
	* X7 Chat 3 is distributed in the hope that it will be useful, but      *
	* WITHOUT ANY WARRANTY; without even the implied warranty of            *
	* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
	* General Public License for more details.                              *
	*                                                                       *
	* You should have received a copy of the GNU General Public License     *
	* along with this program.  If not, see <http://www.gnu.org/licenses/>. *
	*                                                                       *
	* Additional Terms, compatible with Section 7 of the GNU GPL v3,        *
	* apply to X7 Chat 3 and all X7 Chat 3 files, including this one.       *
	* You should have received a copy of these Additional Terms in the      *
	* file docs/COPYING.ADDITIONAL_TERMS.  If you did not receive a copy    *
	* please visit <http://www.x7chat.com/COPYING.ADDITIONAL_TERMS/>.       *
	************************************************************************/

	class lib_group extends lib_clientsidedb {
		const GUEST_GROUP_GID = 3;
		
		function lib_group($initial_id,$getby='gid'){
			if(!is_numeric($initial_id) && $getby == 'gid') $getby = 'name';
			parent::lib_clientsidedb($initial_id,$getby,'gid','groups');
		}

		function get_output(){
			return $this->data_cache;
		}
		
		function commit_changes($changes=null){
			if($changes !== null)
				$this->data_cache = array_merge($this->data_cache,$changes);
			
			parent::commit_changes();
		}
		
		public function set_permission($permission,$mode,$pgroup=null){
			
			if($pgroup == null){
				if(!isset($GLOBALS['PERMS'][$permission]))
					trigger_error($GLOBALS['LANG']->lang_global->permission_does_not_exist.$permission,E_USER_ERROR);	
			
				return $this->set_permission($GLOBALS['PERMS'][$permission][1],$mode,$GLOBALS['PERMS'][$permission][0]);
			}
			
			if(!isset($this->data_cache['perms'][$pgroup]))
				$this->data_cache['perms'][$pgroup] = array('allowed'=>0,'denied'=>0);
				
			$this->data_cache['perms'][$pgroup]['allowed'] &=~ $permission;
			$this->data_cache['perms'][$pgroup]['denied'] &=~ $permission;
				
			switch($mode){
				case -1:
					// Set to denied
					$this->data_cache['perms'][$pgroup]['denied'] |= $permission;
				break;
				case 1:
					// Set to allow
					$this->data_cache['perms'][$pgroup]['allowed'] |= $permission;
				break;
			}
			
		}
		
		public function local_permission_value($permission,$group=null){
			
			if($group == null){
				if(!isset($GLOBALS['PERMS'][$permission]))
					trigger_error($GLOBALS['LANG']->lang_global->permission_does_not_exist.$permission,E_USER_ERROR);
				
				return $this->local_permission_value($GLOBALS['PERMS'][$permission][1],$GLOBALS['PERMS'][$permission][0]);
			}
				
			if(!isset($this->data_cache['perms'][$group]))
				return 0;
				
			if($permission & $this->data_cache['perms'][$group]['allowed'])
				return 1;
			elseif($permission & $this->data_cache['perms'][$group]['denied'])
				return -1;
			else
				return 0;
			
		}

	}
	
	function create_group($name,$image=''){
		
		$gid = $GLOBALS['DATABASE']->table_next_id('groups');
		$group = new lib_group($gid);
		$group->data_cache = array(	'gid' => $gid,
						'name' => $name,
						'img' => $image,
						'perms'=>array(),
						);
		$group->commit_changes();
		
		return $gid;
	}
?>